﻿Public Class perModulos
    Inherits PersistenciaPadrao.AcessoBD

#Region "Transação"

    Public Sub configurarAberturaConexao()

        If perConexao.SqlComando Is Nothing OrElse perConexao.SqlConexao Is Nothing Then
            MyBase.abrirConexao()
            perConexao.SqlComando = MyBase.retornarComandoSqlServer
            perConexao.SqlConexao = MyBase.retornarConexaoSqlServer
        Else
            MyBase.configurarTransacaoSqlServer(perConexao.SqlComando, perConexao.SqlConexao)
        End If

    End Sub

    Public Sub configurarFechamentoConexao()
        MyBase.fecharConexao()
        perConexao.SqlComando = Nothing
        perConexao.SqlConexao = Nothing
    End Sub

#End Region

#Region "Métodos"

    Public Function listarModulos() As DataTable

        Dim sSql As String = String.Empty

        sSql = " SELECT " &
               "    seg_modulos.* " &
               " FROM " &
               "    seg_modulos " &
               " WHERE " &
               "    codModulo <> 1 "

        Return MyBase.executarConsulta(sSql, True)

    End Function

    Public Function listarUsuarioModulos(ByVal sCodModulos As String) As DataTable

        Dim sSql As String = String.Empty

        sSql = " SELECT DISTINCT " &
               "    seg_modulos.* " &
               " FROM " &
               "    seg_modulos " &
               " INNER JOIN seg_grupos_acesso ON seg_modulos.codModulo = seg_grupos_acesso.codModulo " &
               " WHERE " &
               "    seg_modulos.codModulo <> 1 "

        If Not String.IsNullOrWhiteSpace(sCodModulos) Then
            sSql &= " AND seg_modulos.codModulo NOT IN (" & sCodModulos & ")"
        End If

        Return MyBase.executarConsulta(sSql, True)

    End Function

    Public Function selecionarModulos() As DataTable

        Dim sSql As String = String.Empty

        sSql = " SELECT " &
               "    seg_modulos.codModulo, " &
               "    seg_modulos.descricao, " &
               "    seg_modulos.caminho caminhoExe, " &
               "    seg_modulos.caminhoAtu " &
               " FROM " &
               "    seg_modulos " &
               " WHERE " &
               "    codModulo <> 1 "

        Return MyBase.executarConsulta(sSql, True)

    End Function

    Public Sub editarModulos(ByVal iCodModu As Integer,
                             ByVal sCaminhoExe As String,
                             ByVal sCaminhoAtu As String)

        Dim sSql As String = String.Empty

        sSql = " UPDATE " &
               "    seg_modulos " &
               " SET " &
               "    caminho = @caminho, " &
               "    caminhoAtu = @caminhoAtu " &
               " WHERE " &
               "    codModulo = @codModulo "

        MyBase.limparParametros()
        MyBase.inserirParametro("@caminho", sCaminhoExe)
        MyBase.inserirParametro("@caminhoAtu", sCaminhoAtu)
        MyBase.inserirParametro("@codModulo", iCodModu)

        MyBase.executarAcao(sSql, True)

    End Sub

#End Region

End Class
